package com.sdry.web.controller.cb;

import com.sdry.model.lz.Customer;
import com.sdry.model.tdd.emailRecord.EmailRecord;
import com.sdry.service.cb.ReportFormService;
import com.sdry.service.jyy.UpService;
import com.sdry.service.tdd.emailRecord.EmailRecordService;
import com.sdry.utils.EmailUtils;
import com.sdry.web.controller.zc.ZcBindAndUnbindController;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class RemindUserJob implements Job {
	

    @Autowired
    EmailRecordService emailRecordService;
    @Autowired
    UpService upService;
    //声明一个Logger
  	private final static Logger logger = LoggerFactory.getLogger(ZcBindAndUnbindController.class);

    public void execute(JobExecutionContext context) throws JobExecutionException{

        System.out.println("----我执行了发送邮件定时，时间是---" + new Date());
        EmailRecord emailRecord = new EmailRecord();
        emailRecord.setRemark("0");
        List<EmailRecord> emailRecordList = emailRecordService.queryAllByMution(emailRecord);
        int num = sendQQMailNumController.sendQQMail_num;
        logger.info("邮件发送个数："+num);
        if (emailRecordList.size() > num) {
        	//所有邮件接收人
        	Map<Long, List<EmailRecord>> map = emailRecordList.stream().collect(Collectors.groupingBy(EmailRecord::getReceivePeopleId));
        	//遍历集合
        	for(Map.Entry<Long, List<EmailRecord>> entry : map.entrySet()){
        		Long key = entry.getKey();
	            String emailContent = "";
        		List<EmailRecord> list=(List<EmailRecord>)entry.getValue();
	            for (EmailRecord emailRecord2 : list) {
	            	emailContent = emailContent + emailRecord2.getEmailContent()+";";
				}
	            Customer customer = upService.queryCustomerEmailById(key);
	            /* ****************************t 优化项 添加 判断是否发送邮件 2019-11-13**************************** */
	            //查询到客户且客户是发送邮件的状态
	            if(null != customer && "1".equals(customer.getIsSendEmail())){
	            	try {
	            		logger.info("邮件发送邮箱："+customer.getCustomer_email()+";内容："+emailContent);
	            		//邮件发送
	            		EmailUtils.sendQQMail(customer.getCustomer_email(), "库存报警", emailContent);
	            		for (EmailRecord emailRecord2 : list) {
	            			//修改状态为已发送
	            			emailRecord2.setRemark("1");
	            			emailRecordService.update(emailRecord2);
	            		}
	            	} catch (Exception e) {
	            		e.printStackTrace();
	            	}
	            }
        	}
        	/*for (EmailRecord emailRecord2 : emailRecordList) {
                Customer customer = upService.queryCustomerEmailById(emailRecord2.getReceivePeopleId());
                try {
                    //邮件发送
                    EmailUtils.sendQQMail(customer.getCustomer_email(), "库存报警", emailRecord2.getEmailContent());
                    //修改状态为已发送
                    emailRecord2.setRemark("1");
                    emailRecordService.update(emailRecord2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }*/
        }
    }


}
